<?php
include( "function.php" );
class CTopic {
    static function showall($page_list){      
            global $pageContent, $page, $topic;            
            $info = $topic->get_all_topic_info($page_list);
            $userId = $_SESSION["id"];
            /*MAKE TOPICS CONTROL PANEL */
            $xtpl = new XTemplate('topics_info.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                           
            $all_topics_content = "";

            /*GET TABLE HEADER*/
            $ext_content = "";
            $xtpl = new XTemplate('topics_info.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');            
            if (have_permission($groupid=1)){
                $xtpl->assign('edit_reg',"EDIT TOPIC");
                $xtpl->parse( 'sgv' );
                $ext_content = $xtpl->text('sgv');
            }else if (have_permission($groupid=2)){
                $xtpl->assign('edit_reg',"STATUS");
                $xtpl->parse( 'sgv' );
                $ext_content = $xtpl->text('sgv');
            }
            $xtpl->assign('EXT', $ext_content);
            $xtpl->parse( 'header' );
            $all_topics_content = $all_topics_content.$xtpl->text('header');

            for($i=0; $i< count($info); $i++){               
               $all_topics_content = $all_topics_content.get_topic_info_html($alt= $i%2+1, $info[$i]);
            }
                        
            /*MAKE PAGES NAVIGATOR*/
            $page_count = $topic->get_page_count();
            $page_nav_html = get_page_nav_html($page_list, $page_count);
            
            try{
                    $xtpl = new XTemplate('home.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                    $xtpl->assign('TOPICS', $all_topics_content);

                    if (have_permission($groupid=1)){
                        $links = '<div ><a class="button" style="float:left;"  href="'.ABS_URL.'?page=topic1&act=addTopic"'.'">Add new topic</a></div>';
                        $xtpl->assign('ADD_TOPIC_BTN', $links);
                    }
                    $xtpl->assign('PAGE_NAV', $page_nav_html);
                    $xtpl->parse( 'main' );
                    $pageContent= $xtpl->text('main');
                
            }catch (Exception $e){
                $pageContent = '<div id="warning" style="display:block">'.$e->getMessage().'</div>';
            }
}

    static function reg_Topic($topicId){                
                global $pageContent, $page, $topic,$user;               
                $xtpl = new XTemplate('register.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                $links = '<a href="?page=topic1&act=showall" class="link">Back to topics manager</a>';
                $xtpl->assign('TOOLBAR_LINKS', $links);
                $userId = $_SESSION['id'];
                $info = $topic->get_topic_info($topicId);
                
                if ($info['status']<2){
                    if ($user->check_topicid($userId,$topicId)){
                        try{
                            $user->enroll_topic($topicId,$userId);
                            $xtpl->assign('MSG', '<div id="msg">You have been registered this topic!</div>');
                            $xtpl->parse( 'main' );
                            $pageContent= $xtpl->text('main');
                        }catch(Exception $e){
                            $xtpl->assign('MSG', '<div id="warning" style="display:block;">'.$e->getMessage().'</div>');
                            $xtpl->parse( 'main' );
                            $pageContent= $xtpl->text('main');
                        }
                    }
                    else {
                        $pageContent = '<div id="msg" style="display:block;">You have been already registered this topic!</div>';
                    }
                }else if ($info['accept_student'] == $userId){
                    $pageContent = '<div id="msg" style="display:block;">You have been already accepted in this topic!</div>';
                }else{
                    $pageContent = '<div id="msg" style="display:block;">This topic is not registerable!</div>';
                }
    }

    static function unreg_Topic($topicId){
                global $pageContent, $page, $topic,$user;
                $xtpl = new XTemplate('unregister.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                $links = '<a href="?page=topic1&act=showall" class="link">Back to topics manager</a>';
                $xtpl->assign('TOOLBAR_LINKS', $links);
                $userId = $_SESSION['id'];
                $info = $topic->get_topic_info($topicId);
                                    
                try{
                    $user->enroll_topic(0,$userId);
                    $xtpl->assign('MSG', '<div id="msg">You have been unregistered this topic!</div>');
                    $xtpl->parse( 'main' );
                    $pageContent= $xtpl->text('main');
                }catch(Exception $e){
                    $xtpl->assign('MSG', '<div id="warning" style="display:block;">'.$e->getMessage().'</div>');
                    $xtpl->parse( 'main' );
                    $pageContent= $xtpl->text('main');
                }                                    
    }

    static function accept_Topic($topicId){
            global $pageContent, $page, $topic, $user;
            $page_list  = isset($_REQUEST["inpage"]) && $_REQUEST["inpage"]>0? $_REQUEST['inpage'] : "1";
            $info = $user->get_user_by_topic($topicId);
            $userId = $_SESSION["id"];
            $all_sv_content = "";
            /*GET TABLE HEADER*/
            $xtpl = new XTemplate('sv_info.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
            $xtpl->parse( 'header' );
            $all_sv_content = $all_sv_content.$xtpl->text('header');

            for($i=0; $i< count($info); $i++){
               $all_sv_content = $all_sv_content.get_sv_info_html($alt= $i%2+1, $info[$i],$topicId);
            }

            /*MAKE PAGES NAVIGATOR*/
            $page_count = $topic->get_sv_count($topicId);
            $page_nav_html = get_page_nav_html($page_list, $page_count);

            try{
                    $xtpl = new XTemplate('accept.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                    $xtpl->assign('SV', $all_sv_content);
                    $links = 'href="?page=topic1&act=showall"';
                    $xtpl->assign('BACK_HOME',$links);
                    $xtpl->assign('PAGE_NAV', $page_nav_html);
                    $xtpl->parse( 'main' );
                    $pageContent= $xtpl->text('main');

            }catch (Exception $e){
                $pageContent = '<div id="warning" style="display:block">'.$e->getMessage().'</div>';
            }
    }

    static function ok($topicId,$svId){
        global $pageContent, $page, $topic,$user;
        $xtpl = new XTemplate('ok.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
        $links = '<a href="?page=topic1&act=showall" class="link">Back to topics manager</a>';
        $xtpl->assign('TOOLBAR_LINKS', $links);               

// update cai de tai: sua trang thai status thanh 2, accept_student svId
//        
        try{
            $topic->set_student_for_topic($topicId,$svId,2);
            $xtpl->assign('MSG', '<div id="msg">You have been accepted a student enrolling for your topic!</div>');
            $xtpl->parse( 'main' );
            $pageContent= $xtpl->text('main');
        }catch(Exception $e){
            $xtpl->assign('MSG', '<div id="warning" style="display:block;">'.$e->getMessage().'</div>');
            $xtpl->parse( 'main' );
            $pageContent= $xtpl->text('main');
        }
}            

    static function notOk($topicId,$svId){
        global $pageContent, $page, $topic,$user;
        $xtpl = new XTemplate('notOk.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
        $links = '<a href="?page=topic1&act=showall" class="link">Back to topics manager</a>';
        $xtpl->assign('TOOLBAR_LINKS', $links);

// update cai de tai: sua trang thai status thanh 2, accept_student svId
//
        try{
            $topic->set_student_for_topic($topicId,null,1);
            $xtpl->assign('MSG', '<div id="msg">You have been roll out a student from your topic!</div>');
            $xtpl->parse( 'main' );
            $pageContent= $xtpl->text('main');
        }catch(Exception $e){
            $xtpl->assign('MSG', '<div id="warning" style="display:block;">'.$e->getMessage().'</div>');
            $xtpl->parse( 'main' );
            $pageContent= $xtpl->text('main');
        }
}

    static function add_Topic(){
        $userid=$_SESSION['id'];
        global $pageContent, $page, $user, $topic;
        $msg="";
   
        $xtpl = new XTemplate('addtopic.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
        $back_link = ABS_URL.'?page=topic1&act=showall';
        $xtpl->assign('BACK_HOME', $back_link);
        if (have_permission($groupid=1)){ //HAVE ADMIN PERM
            if(isset($_POST['submit'])){
               
                $name = trim($_POST['topicName']);
                $infor = trim($_POST['topicInfor']);
                $userid = $_SESSION["id"];

                try{
                    
                    $topic->insert_topic($userid,$name,$infor);
                    $xtpl->assign('MSG', $msg);
                    $xtpl->assign('TOOLBAR_LINKS', '<a href="'.ABS_URL.'?page=topic1&act=showall" class="link">Back to view topic</a><a href="" class="link">Add another</a>');
                    $xtpl->parse( 'success_msg' );

                    $pageContent= $xtpl->text('success_msg');
                    unset($_POST);
                    return;

                } catch (Exception $e) {
                    $msg  = '<div id="warning" style="display:block;">'.$e->getCode()." : ".$e->getMessage().'</div>';
                }
            }

            $xtpl->assign('MSG', $msg);
            $xtpl->assign('GROUPBOX', create_group_dropbox($selected=0));
            $xtpl->parse( 'main' );
            $pageContent= $xtpl->text('main');
        }else{
            $pageContent = '<div id="msg" style="display:block;">No permission!</div>';
        }
    }

    static function edit_Topic($topicId){
        $userId=$_SESSION['id'];
        global $pageContent, $page, $user, $topic;
        $msg="";
        $info = $topic->get_topic_info($topicId);
        $xtpl = new XTemplate('edittopic.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
        $id = trim($info['giaovien_id']);
        
        if (have_permission($groupid=1)){ //HAVE ADMIN PERM
            if ($id==$userId){
                $topicName = trim($info['name']);
                $topicInfor = trim($info['thongtin_detai']);
                $delete_link = ABS_URL.'?page=topic1&act=deleteTopic&topicId='.$topicId;
                $back_link = ABS_URL.'?page=topic1&act=showall';
                
                $xtpl->assign('DELETE_LINK', $delete_link);
                $xtpl->assign('BACK_HOME', $back_link);                
                $xtpl->assign('TOPICNAME', $topicName);
                $xtpl->assign('TOPICINFOR', $topicInfor);

                if (isset($_POST['submit'])){
                    try{
                        $topicName=trim($_POST['topicName']);
                        $topicInfor=trim($_POST['topicInfor']);

                        $topic->edit_exist_topic($topicId,$topicName,$topicInfor);
                        $xtpl->assign('TOOLBAR_LINKS', '<a href="'.ABS_URL.'?page=topic1&act=users" class="link">Back to topics manager</a>');
                        $xtpl->parse( 'success_msg' );
                        $pageContent= $xtpl->text('success_msg');
                        unset($_POST);
                        return;

                    } catch (Exception $e) {
                        $msg  = '<div id="warning" style="display:block;">'.$e->getCode()." : ".$e->getMessage().'</div>';
                    }
                }

                $xtpl->assign('MSG', $msg);
                $xtpl->parse( 'main' );
                $pageContent= $xtpl->text('main');
            }else{
                $pageContent = '<div id="msg" style="display:block;">You do not have permission with this topic!</div>';
            }
        }else{
            $pageContent = '<div id="msg" style="display:block;">No permission!</div>';
        }
    }

    static function view_Topic($topicId){
                global $pageContent, $page, $user,$topic;
                if ($_SESSION['username']){                    
                    $xtpl = new XTemplate('topicinfo.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');                    
                    $userId= $_SESSION['id'];                    
                    $back_link = ABS_URL.'?page=topic1&act=showall';
                    $info = $topic->get_topic_info($topicId);
                    $id=$info["id"];
                    $amountReg=$topic->get_students_count($id);

                    $xtpl->assign('TOPICNAME', $info["name"]);
                    $xtpl->assign('ADVISERNAME', $user->get_display_name($info["giaovien_id"]));                    
                    $xtpl->assign('TOPICINFOR', $info["thongtin_detai"]);
                    $xtpl->assign('STUDENTREG', $amountReg);
                    $xtpl->assign('BACK_HOME', $back_link);
                    $id = trim($info['giaovien_id']);
                    if ($id == $userId){                        
                            $links = '<a href="?page=topic1&act=editTopic&topicId='.$topicId.'" class="link">Edit this topic</a>
                              <a href="?page=topic1&act=deleteTopic&topicId='.$topicId.'" class="link"
                                  onclick="return confirm(\'Are you sure want to delete this topic?\')">  <font color="red">Delete this topic</font></a>';
                            $xtpl->assign('LINKS', $links);      
                    }
                    
                    if (have_permission($groupid=2)){ // sinh vien permission
                        if ($user->check_topicid($userId,$topicId)){
                            $reg_link = '<a href="?page=topic1&act=regTopic&topicId='.$topicId.'" class="link"
                                        onclick="return confirm(\'Are you sure want to register this topic?\')">  <font color="white">Register this topic</font></a>';
                            $xtpl->assign('LINKS',$reg_link);     // link dang ky topic
                        } else {
                            $un_reg_link = '<a href="?page=topic1&act=unregTopic&topicId='.$topicId.'" class="link"
                                        onclick="return confirm(\'Are you sure want to unregister this topic?\')">  <font color="red">Unregister this topic</font></a>';
                            $xtpl->assign('LINKS',$un_reg_link);     // link huy dang ky topic
                        }
                    }
                    $xtpl->parse( 'main' );
                    $pageContent= $xtpl->text('main');
                }else{
                    $pageContent = '<div id="msg" style="display:block;">No permission!</div>';
                }

    }

    static function delete_Topic($topicId){
                $userId = $_SESSION['id'];
                global $pageContent, $page, $topic;
                $xtpl = new XTemplate('deletetopic.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
                $links = '<a href="?page=topic1&act=showall" class="link">Back to topics manager</a>';
                $xtpl->assign('TOOLBAR_LINKS', $links);
                $info = $topic->get_topic_info($topicId);
                $id = trim($info['giaovien_id']);
                if ($id == $userId)
                {
                    try{
                        if ($topic->delete_topic($topicId)){
                            $xtpl->assign('MSG', '<div id="msg">One topic have been deleted!</div>');
                            $xtpl->parse( 'main' );
                            $pageContent= $xtpl->text('main');
                        }
                    }catch(Exception $e){
                        $xtpl->assign('MSG', '<div id="warning" style="display:block;">'.$e->getMessage().'</div>');
                        $xtpl->parse( 'main' );
                        $pageContent= $xtpl->text('main');
                    }
                }else{
                    $pageContent = '<div id="msg" style="display:block;">You do not have permission with this topic!</div>';
                }
    }
        
}
?>